package com.samphanie.security.config;

import com.samphanie.security.security.service.OauthClientService;
import com.samphanie.security.security.service.impl.DefaultOauthClientServiceImpl;
import com.samphanie.security.security.service.impl.DefaultUserDetailServiceImpl;
import com.samphanie.security.security.service.impl.DefaultUserDetailsPasswordServiceImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * 认证相关的扩展点配置:
 * 配置在这里的bean，业务系统都可以通过声明同类型或同名的bean来覆盖安全模块默认的配置。
 *
 * @author Dries
 * @email 1451691457@qq.com
 */
@Configuration
public class AuthenticationBeanConfig {

    /**
     * 默认密码处理器
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

    /**
     * 默认认证器
     *
     * @return DefaultUserDetailServiceImpl
     */
    @Bean
    @ConditionalOnMissingBean(UserDetailsService.class)
    public UserDetailsService userDetailsService() {
        return new DefaultUserDetailServiceImpl();
    }

    @Bean
    @ConditionalOnMissingBean(UserDetailsPasswordService.class)
    public UserDetailsPasswordService userDetailsPasswordService() {
        return new DefaultUserDetailsPasswordServiceImpl();
    }

    @Bean
    @ConditionalOnMissingBean(OauthClientService.class)
    public OauthClientService oauthClientService() {
        return new DefaultOauthClientServiceImpl();
    }
}
